/*
 * Copyright (c) 2009 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ${basePackage}.action.admin;

import java.util.List;
import ${basePackage}.action.BaseActionBean;
import ${basePackage}.model.Person;
import ${basePackage}.view.PersonCount;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.HttpCache;
import net.sourceforge.stripes.action.SimpleMessage;
import org.rayures.view.View;
import org.rayures.view.stripes.SForm;
import org.rayures.view.stripes.SHidden;
import org.rayures.view.stripes.SLink;
import org.rayures.view.stripes.SSubmit;
import org.rayures.view.stripes.STableForm;
import org.rayures.view.stripes.SText;
import org.springframework.stereotype.Component;

@HttpCache(allow=false)
@Component
public class PersonActionBean extends BaseActionBean {

  public PersonActionBean() {
    System.out.println("@@@@@@@@@@ new PersonActionBean()");
  }

  // Create new Person object to avoid NPE when all fields are blank
  public Person person = new Person();

  public List<Person> getPeople() {
    return dao.loadAll(Person.class, "name desc");
  }

  public Object getPersonTable() {
    return tag(PersonTable.class).init(getClass(), getPeople());
  }

  public View getIndexView() {
    View view = new View();

    view.put("personTable", getPersonTable());

    // This is not a mistake: the add link points to the edit event handler.
    // The action is the same; the add link just doesn't have an associated personId.
    view.put("addLink", tag(SLink.class).event("edit").label(bundle.get("add")));

    view.put("personCount", tag(PersonCount.class));

    return view;
  }

  @Override
  protected void onIndex() {
    System.out.println("prepare index");
  }

  public void edit() {
    // test overriding default forward
    if (System.currentTimeMillis() % 2 == 0) {
      forward(getClass(), "index");
    }
  }

  public View getEditView() throws Exception {
    View view = new View();

    SForm form = tag(STableForm.class)
      .legend("Please enter your information")
      .add(tag(SHidden.class, "person"))
      .addInputs(tag(SText.class, "person.name"), tag(SText.class, "person.birthDate"))
      .addButtons(tag(SSubmit.class, "save"), tag(SSubmit.class, "cancel"));

    view.put("personForm", form);

    return view;
  }

  public void save() {
    dao.save(person);
    getContext().getMessages().add(new SimpleMessage(person.getName() + " was saved successfully."));
    redirectToIndex();
  }

  public void remove() {
    dao.remove(person);
    getContext().getMessages().add(new SimpleMessage(person.getName() + " has been removed."));
    redirectToIndex();
  }

  @DontBind
  public void cancel() {
    redirectToIndex();
  }
}
